//
//  PopBaseViewController.m
//  KidReading
//
//  Created by telen on 15/2/9.
//  Copyright (c) 2015年 刘赞黄Telen. All rights reserved.
//

#import "PopBaseViewController.h"
#import "ConstDefine.h"

@interface PopBaseViewController ()

@end

@implementation PopBaseViewController
{
    CGSize size;
    NSArray *_constraints;
    BaseAnimation* _animation;
}

- (instancetype)init
{
    if (self = [super init]) {
        [self initSize];
        _animation = [[ModalAnimation alloc] init];
        _animation.delegate = self;
        //
        self.view.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view lyt_setSize:size];
        //[self.view lyt_alignToParent];
        if (!_constraints && self.view.superview) _constraints = [self.view lyt_centerInParent];
        //
        [self addContetView];
    }
    return self;
}

- (void)initSize
{
    //size = CGSizeMake(426/ratio_w, 500/ratio_w);
    size = CGSizeMake(540/ratio_w, 620/ratio_w);
    if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
        size.width = width_my;
        size.height = height_my;
    }
}

- (void)addContetView
{
    if (!_ctView) {
        UIImage* image = [UIImage imageNamed:@"setting_top.png"];
        UIView* ctview = [[UIView alloc] init];
        ctview.backgroundColor = [UIColor clearColor];
        _ctView = ctview;
        [self.view addSubview:ctview];
        CGFloat pod = floor(48/ratio_w);
        _ctSize = CGSizeMake(size.width-2*pod, ceil(size.height-image.size.height-pod*0.5));
        ctview.translatesAutoresizingMaskIntoConstraints = NO;
        [ctview lyt_alignTopToParentWithMargin:image.size.height];
        [ctview lyt_alignLeftToParentWithMargin:pod];
        [ctview lyt_setSize:_ctSize];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSize];
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.clipsToBounds = YES;
    self.view.layer.cornerRadius = 12/ratio_w;
    self.view.layer.masksToBounds = YES;
    //
    [self addContetView];
    //
    UIImage* image = [UIImage imageNamed:@"setting_top.png"];
    UIImageView* imgv = [[UIImageView alloc] initWithImage:image];
    imgv.backgroundColor = [UIColor clearColor];
    [self.view addSubview:imgv];
    imgv.translatesAutoresizingMaskIntoConstraints = NO;
    [imgv lyt_alignTopToParent];
    [imgv lyt_alignLeftToParent];
    [imgv lyt_alignRightToParent];
    CGFloat h = image.size.height;
    if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone && ![DataProcessing shareSingle].isStatusHidden) {
        h = image.size.height + 8;
    }
    [imgv lyt_setHeight:h];
    //
    [self addBackView];
}

- (void)addSuperViewConstraints
{
    if (!_constraints && self.view.superview) _constraints = [self.view lyt_centerInParent];
    //NSLog(@"self.view.superview %@",self.view.superview);
}

- (void)popUrl:(NSString*)uri
{
    PopBaseViewController* popVc = [[PopBaseViewController alloc] init];
    PopWebView* webview = [[PopWebView alloc] initWithFrame:self.view.bounds];
    webview.isClickToBrower = YES;
    [webview.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:uri]]];
    [popVc.ctView addSubview:webview];
    [webview autoLayOut_FullWebView];
    [self presentToPopViewController:popVc];
}

- (void)presentToPopViewController:(PopBaseViewController*)popvc
{
    if (popvc) {
        BOOL isModal_OK = [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0;
        if (isModal_OK) {
            popvc.modalPresentationStyle = UIModalPresentationCustom;
            popvc.transitioningDelegate = self;
        }else{
            popvc.modalPresentationStyle = UIModalPresentationNone;
        }
        [self presentViewController:popvc animated:YES completion:nil];
    }
}

- (void)vcTransitions_AnimationPresent_Begin:(UIViewController *)presentVC
{
    PopBaseViewController* popvc = (PopBaseViewController*)presentVC;
    [popvc addSuperViewConstraints];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    _animation.type =  AnimationTypePresent;
    return _animation;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    _animation.type =  AnimationTypeDismiss;
    return _animation;
}

- (void)addBackView
{
    CGFloat w = 74/ratio_w;
    CGFloat h = 64/ratio_w;
    CGFloat wh_img = 44/ratio_w;
    if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
        w = ceil(w*1.35);
        h = ceil(h*1.35);
        if (![DataProcessing shareSingle].isStatusHidden) {
            h = h + 20;
        }
        wh_img = ceil(wh_img*1.35);
    }
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
    view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:view];
    //
    TLTapImageView* imgv = [[TLTapImageView alloc] initWithFrame:CGRectMake(0, 0, wh_img, wh_img)];
    imgv.normalImage = [UIImage imageNamed:@"setting_shut_btn_normal.png"];
    imgv.selectImage = [UIImage imageNamed:@"setting_shut_btn_highlight.png"];
    imgv.image = imgv.normalImage;
    imgv.center = CGPointMake(w/2, h/2);
    [view addSubview:imgv];
    //
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBack)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [view addGestureRecognizer:tap];
    tap.delegate = [TLGestureRecognizerDelegate shareSingleTapView];
}

- (void)tapBack
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [self dealloc_My];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showMessage:(NSString*)str
{
    BOOL isModal_OK = [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0;
    if (isModal_OK) {
        [[WaitingShow shareSingleShow] showString:str inSuperView:self.view.window andAutoHide:YES];
    }else{
        [[WaitingShow shareSingleShow] showString:str inSuperView:self.view andAutoHide:YES];
    }
}

@end
